﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->treeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);

    ui->treeWidget->clear();
    ui->treeWidget->setColumnCount(2);

    ui->treeWidget->setHeaderLabels(QStringList({"科目","描述"}));

    QTreeWidgetItem*root1=new QTreeWidgetItem(ui->treeWidget);
    root1->setText(0,"c语言");
    root1->setText(1,"开发基础");
    QIcon icon;
    icon.addFile(":/mmexport1741683421050.jpg");
    root1->setIcon(0,icon);
    ui->treeWidget->addTopLevelItem(root1);


    QStringList list={"指针","程序控制","数据结构","结构体","文件操作"};
    for(int i=0;i<list.size();i++)
    {
        QTreeWidgetItem*item=new QTreeWidgetItem(root1);
        item->setText(0,list[i]);
        item->setText(1,"了解即可");
        root1->addChild(item);
    }

    auto child=root1->child(4);
    QStringList list2={"读文件","写文件","查找文件","格式文件"};
    for(int i=0;i<list2.size();i++)
    {
        QTreeWidgetItem *item=new QTreeWidgetItem(child);
        item->setText(0,list2[i]);
        item->setText(1,"人才啊你");
        child->addChild(item);
    }

    QTreeWidgetItem *root2=new QTreeWidgetItem(ui->treeWidget);
    root2->setText(0,"C++");
    root2->setText(1,"高级开发");
    icon.addFile(":/mmexport1741683421050.jpg");
    root2->setIcon(0,icon);

    ui->treeWidget->addTopLevelItem(root2);
    QStringList list3={"类和对象","继承和派生","运算符重载","虚函数","模板"};
    for(int i=0;i<list3.count();i++)
    {
        QTreeWidgetItem* item=new QTreeWidgetItem(root2);
        item->setText(0,list3[i]);
        item->setText(1,"熟练掌握");
        root2->addChild(item);
    }

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_action_insert_triggered()
{
    auto item=ui->treeWidget->currentItem();
    if(item&&item->isSelected())
    {
        QTreeWidgetItem*newItem=new QTreeWidgetItem();
        newItem->setFlags(Qt::ItemIsEditable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
        newItem->setText(0,"人才");
        newItem->setText(1,"人才");
        auto parent=item->parent();
        if(parent)
        {
            //子
            item->parent()->insertChild(ui->treeWidget->currentIndex().row(),newItem);
        }
        else
        {
            //根
            //item->insertChild(ui->treeWidget->currentIndex().row(),newItem);
            QIcon icon;
            icon.addFile(":/mmexport1741683421050.jpg");
            newItem->setIcon(0,icon);
            ui->treeWidget->insertTopLevelItem(ui->treeWidget->currentIndex().row(),newItem);
        }

    }

//    QTreeWidgetItem*newItem=new QTreeWidgetItem();
//    newItem->setText(0,"插入的条目");
//    item->parent()->insertChild(ui->treeWidget->currentIndex().row(),newItem);
}

void MainWindow::on_action_delete_triggered()
{
    auto item=ui->treeWidget->selectedItems();
    for(auto it=item.begin();it!=item.end();it++)
    {
        auto parent=(*it)->parent();
        if(parent)//子
        {
            int index=parent->indexOfChild(*it);
            parent->takeChild(index);
        }
        else
        {
            int index=ui->treeWidget->indexOfTopLevelItem(*it);
            ui->treeWidget->takeTopLevelItem(index);
        }
    }
}

void MainWindow::on_treeWidget_itemSelectionChanged()
{
    auto items=ui->treeWidget->selectedItems();
    for(auto it=items.constBegin();it!=items.constEnd();it++)
    {
        ui->textEdit->append("已选"+(*it)->text(0)+","+(*it)->text(1));
    }
}
